package com.LA_houseLease.common.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * 自定义redisTemplate(用于做向redis中保存缓存数据以提高性能)
 * Spring-data-redis提供了StringRedisTemplate和RedisTemplate<Object,Object>两个实例，
 * 但是两个实例均不满足我们当前的需求，所以我们需要自定义RedisTemplate
 */
@Configuration
public class RedisConfiguration {

    @Bean
    public RedisTemplate<String,Object> stringObjectRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory); //设置redisTemplate使用的连接工厂
        redisTemplate.setKeySerializer(RedisSerializer.string());   //设置键值对键和值的序列化方式
        redisTemplate.setValueSerializer(RedisSerializer.java());
        return redisTemplate;
    }
}
